#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：employment_class 
@File    ：01_连接mysql.py
@IDE     ：PyCharm 
@Author  ：小邓的烂笔头
@Date    ：2021/10/17 下午3:00 
"""

import pymysql


# def select_db(select_sql):
#     """查询"""
#     # 建立数据库连接
#     db = pymysql.connect(
#         host="127.0.0.1",
#         port=3306,
#         user="root",
#         passwd="123456",
#         db="base"
#     )
#     # 通过 cursor() 创建游标对象，并让查询结果以字典格式输出
#     cur = db.cursor(cursor=pymysql.cursors.DictCursor)
#     # 使用 execute() 执行sql
#     cur.execute(select_sql)
#     # 使用 fetchall() 获取所有查询结果
#     data = cur.fetchall()
#     # 关闭游标
#     cur.close()
#     # 关闭数据库连接
#     db.close()
#     return data
#
# select_sql = 'SELECT * FROM stu ;'
# print(select_db(select_sql))

class Mysql_Db(object):
    def __init__(self):
        self.connect = pymysql.connect(
            host='localhost',
            port=3306,
            user='root',
            password='123456',
            db='base'
        )
        self.cursorl = self.connect.cursor()

    def select_operation(self,sql):
        self.cursorl.execute(sql)
        self.connect.commit()
        return self.cursorl.fetchall()

    def parameterization(self):
        # 参数化
        title = input("qingshuruxueshengxingming:")
        id = 1
        params = [id,title]
        sql ='insert into rooms(id,title) values(%s,%s)'
        count = self.cursorl.execute(sql, params)
        print(count)
        self.connect.commit()


    def close_connect_cursorl(self):
        self.connect.close()
        self.cursorl.close()

if __name__ == '__main__':
    sql = 'select * from stu;'
    mysql = Mysql_Db()
    # print(mysql.select_operation(sql))
    # mysql.close_connect_cursorl()
    mysql.parameterization()
